package com.jwaoo.chat.session.recent.holder;

import android.os.Handler;
import android.view.View;
import android.widget.TextView;
import com.dx.as.db.IDBListener;
import com.dx.as.ui.CircleImageView;
import com.jwaoo.chat.R;
import com.jwaoo.chat.UserInfo;
import com.jwaoo.chat.activity.module.db.DbService;
import com.jwaoo.chat.data.db.ChatDatabase;
import com.jwaoo.chat.fragment.session.bean.SessionBean;
import com.jwaoo.chat.session.recent.SessionCallback;
import com.jwaoo.chat.session.recent.adpter.SessionAdapter;
import com.jwaoo.chat.ui.recyclerview.adpter.BaseQuickAdapter;
import com.jwaoo.chat.ui.recyclerview.holder.BaseViewHolder;
import com.jwaoo.chat.ui.recyclerview.holder.RecyclerViewHolder;
import com.nostra13.universalimageloader.core.ImageLoader;

public abstract class RecentViewHolder extends RecyclerViewHolder<BaseQuickAdapter, BaseViewHolder, SessionBean> {

    private BaseQuickAdapter adapter;
    public RecentViewHolder(BaseQuickAdapter adapter) {
        super(adapter);
        this.adapter = adapter;
    }

    protected CircleImageView imgHead;
    protected TextView tvNickname;
    protected TextView tvAge;
    protected TextView tvLV;
    protected TextView tvDs;
    protected TextView tvOnlineState;
    protected TextView tvDistance;
    protected View tv_sign;
    protected abstract String descOfMsg(SessionBean recent);
// -- 以下接口可由子类调用
    protected final SessionAdapter getSessionAdapter() {
        return (SessionAdapter) adapter;
    }

    @Override
    public void convert(BaseViewHolder holder, SessionBean data, int position, boolean isScrolling) {
        inflate(holder, data);
        refresh(data);
    }

    private void refresh(final SessionBean sessionBean){
        this.tvDs.setText(descOfMsg(sessionBean));
        DbService.getUserInfo(this.tvDs.getContext(), new IDBListener<ChatDatabase>(){
            @Override
            public void onOption(ChatDatabase database) {
                UserInfo userInfo = database.dbMsgDao().queryUserInfo(sessionBean.getFriend());
                loadImage(userInfo == null ? "" : userInfo.getAvatar());
            }
        });
        tv_sign.setVisibility(sessionBean.getUnReadCount() > 0 ? View.VISIBLE : View.GONE);
    }

    private void loadImage(final String url){

        Handler handler = this.imgHead.getHandler();
        if (handler != null){
            handler.post(new Runnable() {
                @Override
                public void run() {
                    ImageLoader.getInstance().displayImage(url,imgHead);
                }
            });
        }
    }

    public void inflate(BaseViewHolder holder, final SessionBean recent) {
        this.imgHead = holder.getView(R.id.img_avatar);
        this.tv_sign = holder.getView(R.id.tv_sign);
        this.tvNickname = holder.getView(R.id.tv_nickname);
        this.tvAge = holder.getView(R.id.tv_age);
        this.tvLV = holder.getView(R.id.tv_lv);
        this.tvDs = holder.getView(R.id.tv_ds);
        this.tvOnlineState = holder.getView(R.id.tv_online_state);
        this.tvDistance = holder.getView(R.id.tv_distance);
        this.tvNickname.setText(recent.getNickName());
    }

    protected SessionCallback getCallback() {
        return ((SessionAdapter) getAdapter()).getCallback();
    }
}
